﻿#include "qmlporxyconfig.h"

QmlPorxyConfig::QmlPorxyConfig(QObject *parent)
    : QObject{parent}
{
    m_PorxyState = false;
    m_serverPort = 0;
    m_settingsPath = QCoreApplication::applicationDirPath() + "/" + "settings.ini";
}

QmlPorxyConfig *QmlPorxyConfig::getInstance()
{
    static QmlPorxyConfig *myQmlPorxyConfig = new QmlPorxyConfig();
    return myQmlPorxyConfig;
}

int QmlPorxyConfig::start()
{
    if (m_serverPort <= 0 || m_serverPort > 65535) {
        log(QString("Port %1 is out of range.").arg(m_serverPort));
        return -1;
    }
    if (m_PorxyState == true) {
        log(QString("Porxy Server is already started."));
        return -2;
    }
    emit startSignal(m_serverPort);
    return 0;
}

int QmlPorxyConfig::stop()
{
    if (m_PorxyState == false) {
        log(QString("Porxy Server is already stoped."));
        return -1;
    }
    emit stopSignal();
    return 0;
}

bool QmlPorxyConfig::PorxyState() const
{
    return m_PorxyState;
}

void QmlPorxyConfig::setPorxyState(bool newPorxyState)
{
    if (m_PorxyState == newPorxyState)
        return;
    m_PorxyState = newPorxyState;
    emit PorxyStateChanged();
}

int QmlPorxyConfig::serverPort() const
{
    return m_serverPort;
}

void QmlPorxyConfig::setServerPort(int newServerPort)
{
    if (m_serverPort == newServerPort)
        return;
    m_serverPort = newServerPort;
    emit serverPortChanged();
}

QString QmlPorxyConfig::settingsPath() const
{
    return m_settingsPath;
}

void QmlPorxyConfig::setSettingsPath(const QString &newSettingsPath)
{
    if (m_settingsPath == newSettingsPath)
        return;
    m_settingsPath = newSettingsPath;
    emit settingsPathChanged();
}

void QmlPorxyConfig::log(QString s)
{
    QString logstr;
    logstr = "[QmlPorxyConfig]: " + s;
    emit logsPrintSignal(s);
    qDebug() << logstr;
}
